<?php
namespace app\common;

use app\admin\middleware\Lang;
use app\common\exception\AdminException;
use think\facade\Session;

class AdminController extends Controller
{
    protected $model = NULL;

    /**
     * @var array 控制器中间件处理权限与登陆信息
     */
    protected $middleware = [
        \app\admin\middleware\Authentication::class
    ];

    /**
     * @throws AdminException
     */
    public function initialize()
    {
        parent::initialize();
        try {
            $this->initValidatorData();
        } catch (\Exception $e) {
            throw new AdminException($e->getMessage(), -100);
        }
    }

    /**
     * 响应成功JSON
     * @param array $data
     * @param string $code
     * @return \think\response\Json
     */
    public function successRender($data = [], $code = 'success')
    {
        return json([
            'state'     => $code,
            'message'   => 'success',
            'data'      => $data
        ]);
    }

    /**
     * 响应失败请求
     * @param string $message
     * @param string $code
     * @param array $data
     * @return \think\response\Json
     */
    public function errorRender($message = '', $code = 'error', $data = [])
    {
        return json([
            'state'         => $code,
            'message'       => $message,
            'data'          => $data
        ]);
    }

    /**
     * [datalists description]
     * @param  [type] $model [description]
     * @return [type]        [description]
     */
    public function datalists( $isReturn = true )
    {
        $datas = $this->model::dataTable(
            $this->request->param('page', 1, 'intval'),
            $this->request->param('limit', 10, 'intval'),
            $this->request->param('keyword', '','strip_tags')
        );

        return $isReturn === true ? $this->successRender($datas) : $datas;
    }

    /**
     * [saveData description]
     * @return [type] [description]
     */
    public function savedata()
    {
        $data = $this->request->post();
        if ( isset($data['id']) && (int) $data['id'] > 0 ) {
            $model = $this->model::find($data['id']);
            if ( $model ) {
                if (isset($data['passwd']) && empty($data['passwd']) ) {
                    unset($data['passwd']);
                }
                $model->save($data);
                return $this->successRender();
            } else {
                return $this->errorRender('记录不存在');
            }
        }  else {
            $createRs = $this->model::create($data);
            if ( $createRs ) {
                return $this->successRender();
            } else {
                return $this->errorRender('记录不存在');
            }
        }
    }

    /**
     * [delete description]
     * @return [type] [description]
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        $model = $this->model::find($id);
        if ( $model ) {
            $model->delete();
            return $this->successRender();
        } else {
            return $this->errorRender('记录不存在');
        }
    }
}
